package com.shengxian.basic.mylogin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.shengxian.basic.mylogin.entity.UserDenglu;
import com.shengxian.basic.mylogin.entity.baseinfo;
import com.shengxian.basic.mylogin.entity.qiye_renzheng;
import com.shengxian.basic.mylogin.mapper.UserDengLuMapper;
import com.shengxian.basic.mylogin.mapper.baseinfomapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "用户信息管理")
@RestController
@RequestMapping("/baseinfo")
@CrossOrigin
@Slf4j
public class baseinfocontroller {

    @Autowired
    private baseinfomapper mybaseinfomapper;

    @ApiOperation(value = "查找用户 ")
    @PostMapping("find")
    public String findAllTeacher(@RequestBody baseinfo mybaseinfo) {
        QueryWrapper<baseinfo> queryWrapper = new QueryWrapper();
        queryWrapper
                .eq("id", mybaseinfo.getId()).or()
                .eq("name", mybaseinfo.getName()).or()
                .eq("phone", mybaseinfo.getPhone()).or()
                .eq("age", mybaseinfo.getAge()).or()
                .eq("shenfenzheng", mybaseinfo.getShenfenzheng()).or()
                .eq("yinhangka", mybaseinfo.getYinhangka()).or()
                .eq("diqu", mybaseinfo.getDiqu()).or()
                .eq("nickname", mybaseinfo.getNickname()).or()
                .eq("userid", mybaseinfo.getUserid());

        List<baseinfo> baseinfo2 = mybaseinfomapper.selectList(queryWrapper);
        log.info(" == " + baseinfo2);
        return baseinfo2.toString();

    }


    @ApiOperation(value = "增加用户")
    @PostMapping("adduser")
    public void adduser(@RequestBody baseinfo mybaseinfo) {
        Map<String, Object> mymap = new HashMap<>();
        mymap.put("id", mybaseinfo.getId());
        List<baseinfo> baseinfo2 = mybaseinfomapper.selectByMap(mymap);
        if (baseinfo2.isEmpty()) {
            log.info(" 没有    可以添加 ");
            mybaseinfomapper.insert(mybaseinfo);
        } else {
            log.info(" 已经有  不能添加 ");
        }

    }


    @ApiOperation(value = " 修改用户")
    @PostMapping("updateuser")
    public void updateuser(@RequestBody baseinfo mybaseinfo) {

        UpdateWrapper<baseinfo> updateWrapper = new UpdateWrapper();
        updateWrapper.set("name", mybaseinfo.getName())
                .set("phone", mybaseinfo.getPhone())
                .set("age", mybaseinfo.getAge())
                .set("shenfenzheng", mybaseinfo.getShenfenzheng())
                .set("yinhangka", mybaseinfo.getYinhangka())
                .set("diqu", mybaseinfo.getDiqu())
                .set("nickname", mybaseinfo.getNickname())
                .set("userid", mybaseinfo.getUserid())
                .eq("id", mybaseinfo.getId());

        mybaseinfomapper.update(null, updateWrapper);

    }


    @ApiOperation(value = " 删除用户")
    @PostMapping("deleteuser")
    public void deleteuser(@RequestBody baseinfo mybaseinfo) {
        log.info(" 删除用户");
        QueryWrapper<baseinfo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", mybaseinfo.getId())
        ;
//                .or()
//                .eq("name", mybaseinfo.getName()).or()
//                .eq("phone", mybaseinfo.getPhone()).or()
//                .eq("age", mybaseinfo.getAge()).or()
//                .eq("shenfenzheng", mybaseinfo.getShenfenzheng()).or()
//                .eq("yinhangka", mybaseinfo.getYinhangka()).or()
//                .eq("diqu", mybaseinfo.getDiqu()).or()
//                .eq("nickname", mybaseinfo.getNickname()).or()
//                .eq("userid", mybaseinfo.getUserid());
        int count = mybaseinfomapper.delete(queryWrapper);
        System.out.println(count);

    }


}
